package com.example.cs_jump;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.example.cs_jump.Bean.JsonBean;
import com.example.cs_jump.Util.ParsingUrl;
import com.example.cs_jump.Util.RoundedBackgroundSpan;
import com.example.cs_jump.Util.Sqlite;
import com.example.cs_jump.Util.UrlFilter;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.shizhefei.view.largeimage.LargeImageView;

import java.io.IOException;
import java.util.List;

public class DetailedActivity extends AppCompatActivity {
    TextView title, biref, detailed_result, detailed_throwMethod;
    Toolbar toolbar;
    ImageView imageView1, imageView2, imageView3, imageView4, imageView5;
    String id, titlename, brief, tickrate, cover;
    JsonBean jsonBean;
    LargeImageView largeImageView;
    int flag = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detailed);

        Intent intent = getIntent();
        title = findViewById(R.id.detailed_title);
        biref = findViewById(R.id.detailed_brief);
        toolbar = findViewById(R.id.detailed_toolbar);
        largeImageView = findViewById(R.id.detailed_largeimage);
        largeImageView.setImage(R.drawable.fortylions);
        largeImageView.setEnabled(true);
        detailed_result = findViewById(R.id.detailed_result);
        detailed_throwMethod = findViewById(R.id.detailed_throwMethod);

        imageView1 = findViewById(R.id.detailed_image1);
        imageView2 = findViewById(R.id.detailed_image2);
        imageView3 = findViewById(R.id.detailed_image3);
        imageView4 = findViewById(R.id.detailed_image4);
        imageView5 = findViewById(R.id.detailed_image5);

        id = intent.getStringExtra("id");
        titlename = intent.getStringExtra("title");
        brief = intent.getStringExtra("brief");
        tickrate = intent.getStringExtra("tickrate");
        cover = intent.getStringExtra("cover");
        jsonBean = new JsonBean(id, titlename, brief, tickrate, cover);

        title.setText(intent.getStringExtra("title") + "  " + "id:" + id);
        biref.setText(intent.getStringExtra("brief"));
        toolbar.setTitle("详细内容");
        setSupportActionBar(toolbar);
        RequestOptions options = new RequestOptions();
//        options.override(680, 500);
//        options.fitCenter();
        options.skipMemoryCache(true);
        options.error(R.mipmap.ic_launcher);
        options.diskCacheStrategy(DiskCacheStrategy.ALL);
        options.diskCacheStrategy(DiskCacheStrategy.NONE);
        options.diskCacheStrategy(DiskCacheStrategy.DATA);
        options.diskCacheStrategy(DiskCacheStrategy.RESOURCE);

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(500);
                    UrlFilter urlFilter = new UrlFilter(id);

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            detailed_result.setText(urlFilter.getChinese());
                            setButton(urlFilter.throwMethod() + urlFilter.getAuthor());
                            // Log.d("msg",urlFilter.getAuthor()+"作者");
                            // Log.d("msg",urlFilter.getUrl());
//                            Glide.with(getApplicationContext()).load(result[0].trim()).apply(options).into(imageView1);
                            String tags = urlFilter.getUrl();
                            String[] urltags = tags.split("\r\n");
                            int flag = urltags.length;
                            for (int i = 0; i < urltags.length; i++) {
                                Log.d("msg", urltags[i]);
                                if (i == 0) {
                                    Glide.with(getApplicationContext()).load(urltags[i]).apply(options).into(new SimpleTarget<Drawable>() {
                                        @Override
                                        public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                                            imageView1.setImageDrawable(resource);

                                        }
                                    });
                                }
                                if (i == 1) {
                                    Glide.with(getApplicationContext()).load(urltags[i]).apply(options).into(new SimpleTarget<Drawable>() {
                                        @Override
                                        public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                                            imageView2.setImageDrawable(resource);
                                        }
                                    });
                                }
                                if (i == 2) {
                                    Glide.with(getApplicationContext()).load(urltags[i]).apply(options).into(new SimpleTarget<Drawable>() {
                                        @Override
                                        public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                                            imageView3.setImageDrawable(resource);
                                        }
                                    });
                                }
                                if (i == 3) {
                                    Glide.with(getApplicationContext()).load(urltags[i]).apply(options).into(new SimpleTarget<Drawable>() {
                                        @Override
                                        public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                                            imageView4.setImageDrawable(resource);
                                        }
                                    });
                                }
                                if (i == 4) {
                                    Glide.with(getApplicationContext()).load(urltags[i]).apply(options).into(new SimpleTarget<Drawable>() {
                                        @Override
                                        public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                                            imageView5.setImageDrawable(resource);
                                        }
                                    });
                                }
                            }
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }

    public void setButton(String string) {
        String[] tags = string.split("\r\n");
        //detailed_throwMethod
//        for (int i=0;i<tags.length;i++){
//            Log.d("msg",tags[i]);
//        }
        SpannableStringBuilder builder = new SpannableStringBuilder();
        for (String tag : tags) {
            String thisTag = " " + tag + " ";
            builder.append(thisTag);
            RoundedBackgroundSpan span;
            span = new RoundedBackgroundSpan(ContextCompat.getColor(this, R.color.colorAccent), ContextCompat.getColor(this, R.color.white));
            builder.setSpan(span, builder.length() - thisTag.length(), builder.length()
                    , Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            builder.append(" ");
        }
        detailed_throwMethod.setText(builder);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_detailed, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_collect_detailed:
                if (flag == 0) {
                    Toast.makeText(this, "收藏成功", Toast.LENGTH_SHORT).show();
                    flag = 1;
                    Sqlite sqlite = new Sqlite(getApplicationContext());
                    SQLiteDatabase db = sqlite.getWritableDatabase();
                    ContentValues values = new ContentValues();
                    values.put("id", jsonBean.getId());
                    values.put("tickrate", jsonBean.getTickrate());
                    values.put("cover", jsonBean.getCover());
                    values.put("title", jsonBean.getTitle());
                    values.put("brief", jsonBean.getBrief());
                    db.insert("collect", null, values);
                    db.close();
                    sqlite.close();
                    values.clear();
                } else {
                    Toast.makeText(this, "你已经收藏过了", Toast.LENGTH_SHORT).show();
                }

                break;

        }
        return super.onOptionsItemSelected(item);
    }
}